﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;
using System.Collections;

namespace UpFile
{
    public partial class UpFile : Form
    {

        FTPClient ftp = null;
        static string fileName;
        string fileFullName;
        string strsuffix;
        public UpFile()
        {
            InitializeComponent();
            progressBar1.Maximum = 100;
        }
        #region 属性
        private int user_id;
        private string user_name;
        private int branch_id;
        private string NAS_IP;
        public int userid
        {
            set
            {
                userid = value;
            }
            get
            {
                return userid;
            }
        }

        public string username
        {
            set
            {
                username = value;
            }
            get
            {
                return username;
            }
        }

        public int branchid
        {
            set
            {
                branchid = value;
            }
            get
            {
                return branchid;
            }
        }

        public string NASIP
        {
            set
            {
                NAS_IP = value;
            }
            get
            {
                return NAS_IP;
            }
        }
        #endregion
        private void btnFile_Click(object sender, EventArgs e)
        {

            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Multiselect = false;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtFile.Text = openDialog.FileName;
            }
        }

        private void btnUP_Click(object sender, EventArgs e)
        {
            fileFullName = this.txtFile.Text.Trim();
            fileName = fileFullName.Substring(fileFullName.LastIndexOf(@"\") + 1);
            strsuffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();
            btnUP.Enabled = false;
            progressBar1.Value = 0;
            ftp = new FTPClient("192.168.137.1", "", "310", "310", 21);
            if (strsuffix == "exe" || strsuffix == "bat" || strsuffix == "sys" || strsuffix == "com")
            {
                MessageBox.Show("不能上传可执行文件", "提示信息", MessageBoxButtons.OK,
                                  MessageBoxIcon.Question,
                                  MessageBoxDefaultButton.Button1,
                                  0, true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtFile.Text.Trim()))
            {
                MessageBox.Show("请选择一个要上传的文件！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ftp.GetFileSize(fileName) != 0)
            {
                MessageBox.Show("服务器上已经存在一个相同的文件！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    SynchronizationContext uiContext = SynchronizationContext.Current;
                    Thread upThread = new Thread(upFile);
                    upThread.Start(uiContext);
                }
                catch (Exception ex)
                {

                }
            }
            btnUP.Enabled = true;
        }
        private void upFile(object state)
        {
            ftp.Put(fileFullName, state, UpdateUI); //上传文件
            if (strsuffix == "jpg" || strsuffix == "bmp" || strsuffix == "gif" || strsuffix == "png")
            {
                Thumbnail();
            }
        }

        public void Thumbnail()
        {
            string pathImageFrom = fileFullName;
            string pathImageTo = "";
            GenThumbnail(pathImageFrom, pathImageTo, 160, 160);

        }
        /// <summary>
        /// This method is executed on the main UI thread.
        /// </summary>
        private void UpdateUI(object state)
        {
            if (state is decimal)
            {
                decimal text = Convert.ToDecimal(state);

                if (progressBar1.Value != progressBar1.Maximum)
                {
                    progressBar1.Value = Convert.ToInt32(text * 100);
                    //int l_value = 100 * (progressBar1.Value - progressBar1.Minimum) / (progressBar1.Maximum - progressBar1.Minimum);
                    labAvge.Text = Convert.ToInt32(progressBar1.Value).ToString() + "%";

                }
            }
            else
            {
                MessageBox.Show(state.ToString());
            }

        }

        #region 生成缩略图
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径（物理路径）</param>
        /// <param name="thumbnailPath">缩略图路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param> 
        public static void GenThumbnail(string pathImageFrom, string pathImageTo, int width, int height)
        {
            Image imageFrom = null;
            try
            {
                imageFrom = Image.FromFile(pathImageFrom);
            }
            catch
            {
                //throw; 
            }
            if (imageFrom == null)
            {
                return;
            }
            // 源图宽度及高度 
            int imageFromWidth = imageFrom.Width;
            int imageFromHeight = imageFrom.Height;
            // 生成的缩略图实际宽度及高度 
            int bitmapWidth = width;
            int bitmapHeight = height;
            // 生成的缩略图在上述"画布"上的位置 
            int X = 0;
            int Y = 0;
            // 根据源图及欲生成的缩略图尺寸,计算缩略图的实际尺寸及其在"画布"上的位置 
            if (bitmapHeight * imageFromWidth > bitmapWidth * imageFromHeight)
            {
                bitmapHeight = imageFromHeight * width / imageFromWidth;
                Y = (height - bitmapHeight) / 2;
            }
            else
            {
                bitmapWidth = imageFromWidth * height / imageFromHeight;
                X = (width - bitmapWidth) / 2;
            }
            // 创建画布 
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            // 用白色清空 
            g.Clear(Color.White);
            // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            // 指定高质量、低速度呈现。 
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            // 在指定位置并且按指定大小绘制指定的 Image 的指定部分。 
            g.DrawImage(imageFrom, new Rectangle(X, Y, bitmapWidth, bitmapHeight), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);
            try
            {
                //经测试 .jpg 格式缩略图大小与质量等最优 
                //bmp.Save(pathImageTo, ImageFormat.Jpeg);
                UpFile upfile = new UpFile();
                int USERID = upfile.user_id;
                int BRANCH_ID = upfile.branch_id;
                Guid guid=new Guid();
                string FILE_NAME = guid.ToString();
                string URL = pathImageTo;
                UpImage.UpFileSoapClient server = new UpImage.UpFileSoapClient();
                server.UpFileRsout(FILE_NAME, USERID, URL, BRANCH_ID);
            }
            catch
            {
            }
            finally
            {
                //显示释放资源 
                imageFrom.Dispose();
                bmp.Dispose();
                g.Dispose();
            }
        }

        #region 参数类
        public class T_PUB_FILE
        {
            public T_PUB_FILE()
            { }
            #region Model
            private string _file_id;
            private string _file_name;
            private string _type;
            private string _url;
            private int? _branch_id;
            private DateTime? _uptime;
            private DateTime? _gettime;
            private int? _userid;
            private string _reamrk;
            /// <summary>
            /// 
            /// </summary>
            public string FILE_ID
            {
                set { _file_id = value; }
                get { return _file_id; }
            }
            /// <summary>
            /// 
            /// </summary>
            public string FILE_NAME
            {
                set { _file_name = value; }
                get { return _file_name; }
            }
            /// <summary>
            /// 
            /// </summary>
            public string TYPE
            {
                set { _type = value; }
                get { return _type; }
            }
            /// <summary>
            /// 
            /// </summary>
            public string URL
            {
                set { _url = value; }
                get { return _url; }
            }
            /// <summary>
            /// 
            /// </summary>
            public int? BRANCH_ID
            {
                set { _branch_id = value; }
                get { return _branch_id; }
            }
            /// <summary>
            /// 
            /// </summary>
            public DateTime? UPTIME
            {
                set { _uptime = value; }
                get { return _uptime; }
            }
            /// <summary>
            /// 
            /// </summary>
            public DateTime? GETTIME
            {
                set { _gettime = value; }
                get { return _gettime; }
            }
            /// <summary>
            /// 
            /// </summary>
            public int? USERID
            {
                set { _userid = value; }
                get { return _userid; }
            }
            /// <summary>
            /// 
            /// </summary>
            public string REAMRK
            {
                set { _reamrk = value; }
                get { return _reamrk; }
            }
            #endregion Model

        }
        #endregion

        private void btncreate_Click(object sender, EventArgs e)
        {
            ftp = new FTPClient("192.168.137.1", "", "310", "310", 21);
            ftp.MkDir("feng");
        }
        #endregion
    }
}
